<?php
class Upload extends CI_Controller {

	function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('user/user_model');
        $this->uid = $this->user_model->get_uid();	
    }
	public function form()
	{
		$uid = $this->user_model->get_uid();
		if($uid == 0)
		{
			redirect(base_url()."tipask/?user/login.html");
			exit();
		}
		$data = array('category'=>array('paper'=>'试卷','answer'=>'答案','textbook'=>'教材','note'=>'笔记','other'=>'其他'),
					  'error'=>" ");
										
		//$this->load->model('share/file');
		$this->load->view('header',array("title"=>"我来分享"));
		$this->load->view('share/upload_form', $data);
		$this->load->view('footer',$data);
	}
	public function do_upload()
	{
		$config['upload_path'] = './uploads/';
		//$config['allowed_types'] = 'pdf|doc|docx|ppt|zip|rar|7z'; // and more here
		$config['allowed_types'] = "*";
		//$config['max_size']	= '20480';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';

		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			
			$data = array('title'=>"资料上传",'category'=>array('paper'=>'试卷','answer'=>'答案','textbook'=>'教材','note'=>'笔记','other'=>'其他'),
							'error' => $this->upload->display_errors());
			
			$this->load->view('header',$data);
			$this->load->view('share/upload_form', $data);
			$this->load->view('footer',$data);
		}
		else
		{
						$category = $_POST['category'];
						$subject = $_POST['subject'];
						$year = $_POST['year'];
						$title = $_POST['title'];
						$description = $_POST['description'];
						$tag = $_POST['tag'];
                        $upload_data = $this->upload->data();
                        $ori_filename = $_FILES['userfile']['name'];
                        $filename = $upload_data['file_name'];
                        $file_path = 'uploads/'.$filename;
						
                        $swf_path = "";
                  		$allowed_type = array('.doc','.ppt','.xls','.docx','.pptx','.xlsx');
                        if($upload_data['file_ext']=='.pdf')
                        {
                        $swf_path = 'uploads/swf/'.$upload_data['raw_name'].'.swf';
                       	$cmd = "pdf2swf $file_path -o $swf_path -p 1-10";
                        shell_exec($cmd);
                        }
                        elseif (in_array($upload_data['file_ext'], $allowed_type))
                        {
                        $swf_path = 'uploads/swf/'.$upload_data['raw_name'].'.swf';
                        $cmd = "jodconverter -f pdf $file_path";
               		    shell_exec("jodconverter -f pdf $file_path");
                        $converted_pdf = 'uploads/'.$upload_data['raw_name'].'.pdf';
                        $cmd = "pdf2swf $converted_pdf -o $swf_path -p 1-10";
                        shell_exec($cmd);
                        unlink($converted_pdf);
                        }
                        //echo shell_exec("pwd");
                        //echo '<br />';
                        //echo $converted_pdf;
                        //echo '<br />';
                        // pdf2swf LICENSE.pdf -o swf/license.swf -p 1-5
                        //shell_exec("pdf2swf uploads/$converted_pdf -o uploads/$swf_path -p 1-5");
                        //$swf_path = shell_exec("pdf2swf $file_path");
			$this->load->model('share/file');
			$this->file->add($this->uid,$file_path,$swf_path,$category,$subject,$year,$title,$tag,$description,$ori_filename);
			$data = array('upload_data' => $this->upload->data(),'swf_path' => base_url().$swf_path);
			
			$this->load->model('user/user_model');
			$this->user_model->refresh_rp($this->uid);
						
			$this->load->view('header',array("title"=>"我来分享"));
			$this->load->view('share/upload_success', $data);
			$this->load->view('footer',$data);
		}
	}
}
?>